<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Anwsion dev team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/


if (!defined('IN_ANWSION'))
{
	die;
}

class favorite_class extends AWS_MODEL
{
	public function add_favorite($question_id, $uid)
	{
		if (!$question_id)
		{
			return false;
		}
		
		if (!$this->count('favorite', 'question_id = ' . intval($question_id) . ' AND uid = ' . intval($uid)))
		{
			return $this->insert('favorite', array(
				'question_id' => intval($question_id),
				'uid' => intval($uid),
				'time' => time()
			));
		}
	}
	
	public function update_favorite_tag($question_id, $tags, $uid)
	{
		if (!$question_id OR !$tags)
		{
			return false;
		}
		
		$tags = str_replace(array('，', ' ', '　'), ',', $tags);
		
		$tags = explode(',', rtrim($tags, ','));
		
		foreach ($tags AS $key => $tag)
		{
			if (!$this->count('favorite_tag', 'question_id = ' . intval($question_id) . ' AND `title` = \'' . $this->quote(htmlspecialchars(trim($tag))) . '\' AND uid = ' . intval($uid)))
			{
				$this->insert('favorite_tag', array(
					'question_id' => intval($question_id),
					'uid' => intval($uid),
					'title' => htmlspecialchars(trim($tag))
				));
			}
		}
		
		return true;
	}
	
	public function remove_favorite_tag($question_id, $tag, $uid)
	{
		if (!$question_id OR !$tag)
		{
			return false;
		}
		
		return $this->delete('favorite_tag', 'title = \'' . $this->quote($tag) . '\' AND question_id = ' . intval($question_id) . ' AND uid = ' . intval($uid));
	}
	
	public function remove_favorite_item($question_id, $uid)
	{
		if (!$question_id OR !$uid)
		{
			return false;
		}
		
		$this->delete('favorite', 'question_id = ' . intval($question_id) . ' AND uid = ' . intval($uid));
		$this->delete('favorite_tag', 'question_id = ' . intval($question_id) . ' AND uid = ' . intval($uid));
	}
	
	public function get_favorite_tags($uid, $limit = null)
	{
		return $this->query_all("SELECT DISTINCT title FROM " . $this->get_table('favorite_tag') . " WHERE uid = " . intval($uid) . ' ORDER BY id DESC', $limit);
	}
	
	public function get_favorite_items_tags_by_question_id($uid, $question_ids)
	{
		if (sizeof($question_ids) == 0 OR !is_array($question_ids))
		{
			return false;
		}
		
		array_walk_recursive($question_ids, 'intval_string');
		
		if ($favorite_tags = $this->fetch_all('favorite_tag', 'uid = ' . intval($uid) . ' AND question_id IN (' . implode(',', $question_ids) . ')'))
		{
			foreach ($favorite_tags AS $key => $val)
			{
				$items_tags[$val['question_id']][] = $val;
			}
		}
		
		return $items_tags;
	}
	
	public function count_favorite_items($uid, $tag = null)
	{
		if ($tag)
		{
			$favorite_items = $this->query_all("SELECT DISTINCT question_id FROM " . get_table('favorite_tag') . " WHERE uid = " . intval($uid) . " AND title = '" . $this->quote($tag) . "'");
			
			return sizeof($favorite_items);
		}
		else
		{
			return $this->count('favorite', 'uid = ' . intval($uid));
		}
	}
	
	public function get_tag_action_list($tag, $uid, $limit)
	{
		if (!$uid)
		{
			return false;
		}
		
		if ($tag)
		{
			$favorite_tags = $this->fetch_all('favorite_tag', '`title` = \'' . $this->quote($tag) . '\' AND uid = ' . intval($uid), 'question_id DESC', $limit);
		}
		else
		{
			$favorite_tags = $this->fetch_all('favorite_tag', 'uid = ' . intval($uid), 'question_id DESC', $limit);
		}
		
		if ($favorite_tags)
		{
			foreach ($favorite_tags AS $key => $val)
			{
				$question_ids[] = $val['question_id'];
			}
		}
		
		if (!$question_ids)
		{
			return false;
		}
		
		$action_list_question_info = array();
		if ($question_ids)
		{
			$action_list_question_info = $this->model('question')->get_question_info_by_ids($question_ids);
		}
		
		return $action_list_question_info;
	}
}